package com.sjh.lib_app.net

import androidx.fragment.app.Fragment
import com.sjh.baseutils.loading.LoadingType
import com.sjh.lib_app.AppConfig
import com.sjh.lib_app.UserConfig
import com.sjh.net.EasyClient
import com.sjh.net.Method
import com.sjh.net.interceptor.BaseInterceptor
import com.sjh.net.interceptor.HttpInfoCatchInterceptor
import com.sjh.net.request

/**
 * 通用的网络请求配置
 * @author Extends
 * @date 2020/6/17
 */

object NetConfig{

    /**
     * app 的整体配置，如果某个模块需要单独的配置，可以参照着来
     */
    fun appConfig(easyClient: EasyClient<*>){
        easyClient.baseUrl = AppConfig.getAppUrl()
        val interceptors = arrayListOf<BaseInterceptor>(HeadInfoInterceptor())
        if(AppConfig.isShowLog()){
            interceptors.add(HttpInfoCatchInterceptor())
        }
//        interceptors.add(EncryptInterceptor())
        easyClient.setDefaultInterceptors(interceptors)
    }
}

inline fun <reified T> androidx.appcompat.app.AppCompatActivity.get(
    preBlock: EasyClient<T>.()->Unit = {NetConfig.appConfig(this)},
    block: EasyClient<T>.()->Unit){
    request(preBlock = preBlock,
        options = {
        method = Method.GET
        lifecycle = this@get.lifecycle
    },block = block)
}

inline fun <reified T> androidx.appcompat.app.AppCompatActivity.post(preBlock: EasyClient<T>.()->Unit = {NetConfig.appConfig(this)}, block: EasyClient<T>.()->Unit){
    request(preBlock = preBlock,options = {
        method = Method.POST
        lifecycle = this@post.lifecycle
    },block = block)
}

inline fun <reified T> Fragment.get(preBlock: EasyClient<T>.()->Unit = {NetConfig.appConfig(this)}, block: EasyClient<T>.()->Unit){
    request(preBlock = preBlock,options = {
        method = Method.GET
        lifecycle = this@get.lifecycle
    },block = block)
}

inline fun <reified T> Fragment.post(preBlock: EasyClient<T>.()->Unit = {NetConfig.appConfig(this)}, block: EasyClient<T>.()->Unit){
    request(preBlock = preBlock,options = {
        method = Method.POST
        lifecycle = this@post.lifecycle
    },block = block)
}